package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogArticleBo;
import com.an.blog.admin.domain.vo.BlogArticleVo;
import com.an.blog.service.IBlogArticleService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 博客文章
 *
 * @author Ann
 * @date 2023-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/article")
public class BlogArticleController extends BaseController {

    private final IBlogArticleService blogArticleService;


    /**
     * 初始化 ES 索引
     *
     * @return {@link R }<{@link Void }>
     * @author Ann
     * @date 2024/06/05
     */
    @SaCheckPermission("blog:article:add")
    @Log(title = "文章管理", businessType = BusinessType.INIT_ES)
    @RepeatSubmit()
    @PostMapping("/initEsIndex")
    public R<Void> initEsIndex( ) {
        return toAjax(blogArticleService.initEsIndex());
    }


    /**
     * 查询博客文章列表
     *
     * @param bo        业务对象
     * @param pageQuery 页面查询
     * @return {@link TableDataInfo }<{@link BlogArticleVo }>
     * @author: Ann
     * @createDate: 2023/05/09 11:38:56
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:list")
    @GetMapping("/list")
    public TableDataInfo<BlogArticleVo> list(BlogArticleBo bo, PageQuery pageQuery) {
        return blogArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取博客文章详细信息
     *
     * @param articleId 主键
     * @return {@link R }<{@link BlogArticleVo }>
     * @author: Ann
     * @createDate: 2023/05/09 11:38:59
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:query")
    @GetMapping("/{articleId}")
    public R<BlogArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long articleId) {
        return R.ok(blogArticleService.queryById(articleId));
    }

    /**
     * 新增博客文章
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 11:39:02
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:add")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.insertByBo(bo));
    }

    /**
     * 修改博客文章
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 11:39:04
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.updateByBo(bo));
    }

    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:49:08
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.updateArticleStatus(bo));
    }

    /**
     * 置顶修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:50:26
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeIsTop")
    public R<Void> changeIsTop(@RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.updateArticleIsTop(bo));
    }


    /**
     * 删除博客文章
     *
     * @param articleIds 主键串
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 11:39:07
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:remove")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] articleIds) {
        return toAjax(blogArticleService.deleteWithValidByIds(Arrays.asList(articleIds), true));
    }
}
